﻿// Copyright (c) 2008 Paul Schaeflein
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in 
// the Software without restriction, including without limitation the rights to use, 
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
// Software, and to permit persons to whom the Software is furnished to do so, subject 
// to the following conditions:

// The above copyright notice and this permission notice shall be included in all 
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Microsoft.SharePoint.StsAdmin;

namespace Schaeflein.Community.StsAdmExtensions
{
  public class ExtensionCommandHandler : ISPStsadmCommand
  {

    #region ISPStsadmCommand Members

    string ISPStsadmCommand.GetHelpMessage(string command)
    {
      command = command.ToLowerInvariant();

      string helpMessage = String.Empty;

      switch (command)
      {
        case "setimageuploadlocation":
          ImageUploadLocation iul = new ImageUploadLocation();
          helpMessage = iul.Help();
          break;

        default:
          helpMessage = "";
          break;
      }

      return helpMessage;
    }

    int ISPStsadmCommand.Run(string command, StringDictionary keyValues, out string output)
    {
      command = command.ToLowerInvariant();

      STSADMResultCodes result;

      switch (command)
      {
        case "setimageuploadlocation":
          //return this.EnumerateFeatures(keyValues, out output);
          ImageUploadLocation iul = new ImageUploadLocation();
          iul.CommandProgress += new ExtensionCommandProgressEventHandler(CommandProgress);
          result = iul.Run(keyValues, out output);
          break;

        default:
          throw new InvalidOperationException();
      }

      return (int)result;

    }

    #endregion

    #region Status Reporting

    void CommandProgress(object sender, ExtensionCommandHandler.ExtensionCommandProgressEventArgs e)
    {
      Console.Out.Write(e.StatusMessage);
    }

    public class ExtensionCommandProgressEventArgs : EventArgs
    {
      private string _statusMessage;
      public string StatusMessage
      {
        get { return _statusMessage; }
      }

      public ExtensionCommandProgressEventArgs() { }

      public ExtensionCommandProgressEventArgs(string StatusMessage)
      {
        _statusMessage = StatusMessage;
      }
    }


    public delegate void ExtensionCommandProgressEventHandler(object sender, ExtensionCommandProgressEventArgs e);

    public delegate void ExtensionCommandCompleteEventHandler(object sender, EventArgs e);

    #endregion

  }
}
